/*
 *  Copyright 2015 the original author or authors.
 *  @https://github.com/scouter-project/scouter
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package scouterx.webapp.framework.session;

import scouter.util.StringKeyLinkedMap;

/**
 * @author Gun Lee (gunlee01@gmail.com) on 2018. 2. 24.
 */
public class UserTokenCache {
    private static StringKeyLinkedMap<UserToken> cache = new StringKeyLinkedMap<UserToken>().setMax(10000);
    private static UserTokenCache instance = new UserTokenCache();

    private UserTokenCache() {}

    public static UserTokenCache getInstance() {
        return instance;
    }

    public UserToken get(UserToken condition) {
        return get(condition.getToken());
    }

    public void put(UserToken token) {
        put(token.getToken(), token);
    }

    public void putAsRecent(UserToken token) {
        cache.putLast(token.getToken(), token);
    }

    private void put(String key, UserToken token) {
        cache.put(key, token);
    }

    private UserToken get(String key) {
        return cache.get(key);
    }
}
